// 加载js文件
export function loadJs(src: string, node: Node = document.body) {
  return new Promise<void>((resolve, reject) => {
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = src;
    node.appendChild(script);
    script.onload = () => {
      resolve();
    };
    script.onerror = () => {
      reject();
    };
  });
}
// 删除js文件
export function unloadJs(src: string, node: Node = document.body) {
  const scripts = document.querySelectorAll('script');
  for (let i = 0; i < scripts.length; i++) {
    const script = scripts[i];
    const srcVal = script.getAttribute('src');
    if (srcVal && srcVal == src) {
      node.removeChild(script);
    }
  }
}

// 加载css文件
export function loadCss(href: string) {
  return new Promise<void>((resolve, reject) => {
    const link = document.createElement('link');
    link.type = 'text/css';
    link.rel = 'stylesheet';
    link.href = href;
    document.head.appendChild(link);

    link.onload = () => {
      resolve();
    };

    link.onerror = () => {
      reject();
    };
  });
}

// 删除css文件
export function unloadCss(href: string, node: Node = document.head) {
  const links = document.querySelectorAll("link[type='text/css']");
  for (let i = 0; i < links.length; i++) {
    const link = links[i];
    const hrefVal = link.getAttribute('href');
    if (hrefVal && hrefVal == href) {
      node.removeChild(link);
    }
  }
}

//时间格式化

export function TimeData(time: any) {
  return `${new Date(time).getFullYear()}-${
    new Date(time).getMonth() + 1
  }-${new Date(time).getDate()}/${new Date(time).getHours()}:${new Date(time).getMinutes()}
        `;
}
